<?php

namespace app\common\com\msgqueue;

define('MSG_QUEUE_ROOT', dirname(__FILE__));

class MsgQueueFactory {

	/**
	 * 获取一个消息队列对象
	 *
     * @param string $type
     * @param array $config
	 * @return MsgQueueImpl
	 */
	public static function getInstance($type = '', array $config = []) {
		static $return = null;
		if($return instanceof MsgQueueImpl) {
			return $return;
		}

        $msgqueue_config = config('extra.msgqueue_config');
        $mq_type         = $msgqueue_config['type'];
        $mq_config       = $msgqueue_config['config'];

        $type = empty($type)   ? $mq_type   : $type;
        $cfg  = empty($config) ? $mq_config : $config;

		require_once (MSG_QUEUE_ROOT . '/interface/' . $type . '.php');
        $return = new $type($cfg);
		if(!$return instanceof MsgQueueImpl) {
			exception('message_queue_interface_invalid');
		}
		return $return;
	}

}